package com.spring.sunflower.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.spring.sunflower.R;
import com.spring.sunflower.bean.TaskItemBean;
import com.spring.sunflower.common.TaskItemAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Kevin on 2021/8/24<br/>
 * <p>
 * Describe:<br/>
 */
public class TaskItemView extends LinearLayout {
    private Context context;
    private String title;
    private TextView tvTitle;
    private RecyclerView recyclerView;
    private TaskItemAdapter mAdapter;
    private List<TaskItemBean> data = new ArrayList<>();

    public TaskItemView(Context context) {
        this(context, null);
    }

    public TaskItemView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TaskItemView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.TaskItemView);
        if (typedArray != null) {
            title = typedArray.getString(R.styleable.TaskItemView_taskTitle);
            typedArray.recycle();
        }
        View view = LayoutInflater.from(context).inflate(R.layout.layout_task_item_view, null);
        tvTitle = view.findViewById(R.id.tvTitle);
        recyclerView = view.findViewById(R.id.rvRecyclerView);
        mAdapter = new TaskItemAdapter(R.layout.adapter_item_taks_item, data);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(mAdapter);
        mAdapter.addChildClickViewIds(R.id.flContainer);
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                List<TaskItemBean> data = mAdapter.getData();
                String taskCode = data.get(position).getTaskCode();
                if (listener != null) {
                    listener.onTaskItemClick(taskCode);
                }
            }
        });
        addView(view);

    }

    public void setTitle(String title) {
        tvTitle.setText(title);
    }

    public void setData(List<TaskItemBean> data) {
        mAdapter.addData(data);
    }

    public void setOnTaskItemClickListener(OnTaskItemClickListener l){
        listener = l;

    }
    private OnTaskItemClickListener listener;
    public interface OnTaskItemClickListener{
        void onTaskItemClick(String taskCode);
    }
}
